package com.hc.controller;

import com.hc.common.bean.PageBean;
import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.common.utils.ControllerUtil;
import com.hc.domain.Technology;
import com.hc.query.SearchVO;
import com.hc.query.TechnologyVO;
import com.hc.res.TechnologyDTO;
import com.hc.service.TechnologyService;
import com.hc.trans.TechnologyTrans;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @ClassName: TechnologyController
 * @Description: 技术模块
 * @date:2021/11/24 17:27
 * @author: zhitb
 * @Version: 1.0
 **/
@Slf4j
@Controller
@RequestMapping("/technology")
public class TechnologyController {
    @Resource
    private TechnologyService technologyService;

    /**
     * 根据查询条件查询启用状态技术信息
     * @param pageNum 当前页
     * @param pageSize 每页数据量
     * @param searchVO 查询条件
     * @param mav 返回数据
     * @return
     */
    @GetMapping("/getTechnologyByCondition/{pageNum}/{pageSize}")
    public ModelAndView getTechnologyByCondition(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            SearchVO searchVO,
            ModelAndView mav,
            String s) {
        TechnologyVO  technologyVO = new TechnologyVO();
        if(!("4".equals(s))){
            technologyVO.setStatus(s);
        }
        // 查找条件封装
        ControllerUtil.initParam(technologyVO, searchVO);
        // 查询技术列表数据
        PageBean<Technology> technologyPageBean = technologyService.selectPageByCondition(pageNum, pageSize, technologyVO);
        // 数据转换
        PageBean<TechnologyDTO> technologyDTOPageBean = TechnologyTrans.MAPPER.technology2TechnologyDTO(technologyPageBean);
        // 存储路径
        technologyDTOPageBean.setUrl("technology/getTechnologyByCondition");
        // 存储返回的 技术列表数据
        mav.addObject("pageBean", technologyDTOPageBean);
        //返回查询条件
        mav.addObject("searchKey", searchVO.getSearchKey());
        mav.addObject("searchValue", searchVO.getSearchValue());
        mav.addObject("title", "技术列表");
        mav.addObject("status",s);
        mav.setViewName("technology/list");
        return mav;
    }
    /**
     * 删除技术
     *
     * @param id
     * @param mav
     * @return
     */
    @ResponseBody
    @DeleteMapping("/removeById/{id}")
    public Result removeById(@PathVariable("id") Long[] id, ModelAndView mav) {
        //删除成功的数量
        int deleteRes = 0;
        //遍历需要删除的项目id数组
        for (int i = 0; i < id.length; i++) {
            //成功删除一次，成功数量加1
            deleteRes += technologyService.deleteByPrimaryKey(id[i]);
        }
        //删除成功的个数和需要删除的个数相同，则删除成功
        if (deleteRes == id.length) {
            return ResultUtil.success().setMsg("删除成功");
        } else {
            return ResultUtil.success().setMsg("删除失败");
        }
    }
    /**
     * 添加技术
     * @param name 技术名
     * @return
     */
    @ResponseBody
    @PostMapping("/addTechnology")
    public Result addTechnology(String name){
        Technology technology=Technology.builder()
                .name(name)
                .status(0)
                .build();
        int i = technologyService.insertSelective(technology);
        if(i==1){
            return ResultUtil.success().setMsg("添加成功！");
        }
        return ResultUtil.fail().setMsg("添加失败！");
    }
    /**
     * 批量启用技术
     *
     * @param id
     * @param mav
     * @return
     */
    @ResponseBody
    @PostMapping("/activeById/{id}")
    public Result activeById(@PathVariable("id") Long[] id, ModelAndView mav) {
        //启用成功的数量
        int deleteRes = 0;
        //遍历需要启用的项目id数组
        for (int i = 0; i < id.length; i++) {
            //成功启用一次，成功数量加1
            deleteRes += technologyService.avctiveByPrimaryKey(id[i]);
        }
        //启用成功的个数和需要启用的个数相同，则启用成功
        if (deleteRes == id.length) {
            return ResultUtil.success().setMsg("启用成功");
        } else {
            return ResultUtil.success().setMsg("启用失败");
        }
    }
    /**
     * 批量禁用技术
     *
     * @param id
     * @param mav
     * @return
     */
    @ResponseBody
    @PostMapping("/disabledById/{id}")
    public Result disabledById(@PathVariable("id") Long[] id, ModelAndView mav) {
        //禁用成功的数量
        int deleteRes = 0;
        //遍历需要禁用的项目id数组
        Technology technology = new Technology();
        for (int i = 0; i < id.length; i++) {
            //成功禁用一次，成功数量加1
            deleteRes += technologyService.disabledByPrimaryKey(id[i]);
        }
        //禁用成功的个数和需要启用的个数相同，则禁用成功
        if (deleteRes == id.length) {
            return ResultUtil.success().setMsg("禁用成功");
        } else {
            return ResultUtil.success().setMsg("禁用失败");
        }
    }
}
